#-*- coding:utf-8 -*-
import os
import sys
import time
import hashlib
import json
import shutil

from PyQt4 import QtGui, QtCore

from common import *

try:
    import cPickle as pickle
except ImportError:
    import pickle

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

def warn_dialog(parent=None, window="MainWindow", title="title", text="text"):
    QtGui.QMessageBox.warning(parent, _translate(window,title, None), \
                    _translate(window,text,None))

def about_dialog(parent=None, window="MainWindow", title="title", text="text"):
    QtGui.QMessageBox.about(parent, _translate(window,title, None), \
                    _translate(window,text,None))

def screen_size():
    screen = QtGui.QDesktopWidget().screenGeometry()
    return screen.height(), screen.width()

def init():
    _init_config()
    _init_temp()

def _init_temp():
    try:
        if os.path.exists(TEMP_PATH) == True:
            shutil.rmtree(TEMP_PATH)
            os.mkdir(TEMP_PATH)
        else:
            os.mkdir(TEMP_PATH)
    except Exception:
        pass

def _init_config():
    if os.path.exists(SETTING_FILE) == False:
        with open(SETTING_FILE, 'w') as setting:
            savedir = os.path.join(os.path.abspath(os.path.dirname(sys.argv[0])),'images')
            d = {}
            d['lockeyes'] = True
            d['savedir'] = savedir
            d['vposcount'] = 60
            d['hposcount'] = 80
            d['calibration'] = False
            json.dump(d, setting, indent=4)


def load_config():
    config = {}
    with open(SETTING_FILE) as jsonfile:
        config = json.load(jsonfile)
    return config


def clb_array():
    d = {}
    with open(CLB_FILE, 'rb') as clb:
        d = json.load(clb)
    return d


def db_array():
    arr = []
    try:
        with open(DATA_FILE, 'rb') as db:
            arr = pickle.load(db)
    except IOError:
        print "database does not exists!"
    return arr


def direct_write_db(arr):
    try:
        with open(DATA_FILE, 'wb') as db:
            pickle.dump(arr, db)
    except IOError:
        print "writing is failed"


def insert_to_db(dict_):
    arr = db_array()
    arr.append(dict_)
    direct_write_db(arr)

def update_db():
    arr = []
    if os.path.exists(DATA_FILE) == False:
        with open(DATA_FILE, 'w') as db:
            pickle.dump([], db)
    else:
        arr = db_array()
    if arr is not None:
        for d in arr:
            if os.path.exists(d.get('path')) == False:
                arr.remove(d)
    direct_write_db(arr)


def get_memory():
    m_linesArr = None
    with open(STUDY_FILE, 'rb') as memory_file:
        m_linesArr = pickle.load(memory_file)[0]
    return m_linesArr

